// forked from Ctrl's Panning + Tits
// forked from Ctrl's Monica Fonseca + Panning
// Monica Fonseca
// By Ctrl
// March 2010
// Inspired By: http://www.soho.com.co/wf_InfoGaleriasMujeres.aspx?IdGal=550

package 
{
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.KeyboardEvent;
	import flash.net.URLRequest;
	import flash.ui.Keyboard;
	
	public class Soho extends Sprite
	{
		protected var model:int = 390;
		protected var image:int = 1;
		protected var url:String = "http://www.soho.com.co/galerias_new/mod_";
		
		protected var errorCount:int = 0;
		
		protected var loader2:Loader = new Loader();
		
		public function Soho()
		{
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			stage.addEventListener(Event.RESIZE, onResize);
			stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
			
			loader2.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError);
			loader2.contentLoaderInfo.addEventListener(Event.COMPLETE, onResize);
			loader2.load(new URLRequest(url+model+"/f"+image+".jpg"));
			addChild(loader2);
		}
		
		protected function onResize(event:Event):void
		{
			if(loader2.content == null) return;
			
			errorCount = 0;
			
			loader2.content.width = stage.stageWidth;
			loader2.content.scaleY = loader2.content.scaleX;
			if(loader2.content.height > stage.stageHeight)
			{
				loader2.content.height = stage.stageHeight;
				loader2.content.scaleX = loader2.content.scaleY;
			}
		}
		
		protected function onError(e:IOErrorEvent):void
		{
			errorCount++;
			
			if(image == 1) model = 390;
			else
			{
				image = 1;
				model++;
			}
			if(errorCount < 5) loader2.load(new URLRequest(url+model+"/f"+image+".jpg"));
		}
		
		protected function onKeyUp(event:KeyboardEvent):void
		{
			switch(event.keyCode)
			{
				case Keyboard.UP:
					model++;
					image = 1;
					if(model > 609) model = 390;
					break;
				case Keyboard.DOWN:
					model--;
					image = 1;
					if(model < 390) model = 609;
					break;
				case Keyboard.LEFT:
					image--;
					if(image < 1) image = 1;
					break;
				case Keyboard.RIGHT:
					image++;
					break;
			}
			loader2.load(new URLRequest(url+model+"/f"+image+".jpg"));
		}
	}
}